// pages/result/result.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ishidden: false,
    hasPakege: true,
    waterImage: [
      "../../image/0@2x.png",
      "../../image/1@.png",
      "../../image/2@2x.png",
      "../../image/3@2x.png",
      "../../image/4@2x.png",
      "../../image/5@2x.png",
      "../../image/6@2x.png",
      "../../image/7@2x.png",
      "../../image/8@2x.png",
      "../../image/9@2x.png",
      "../../image/10@2x.png",
    ],
    resultDiscreption: [
      "警察叔叔，我要报警，我交到假朋友了！",
      "够了，我们的友谊小船已经翻了",
      "难道你就是传说中的表面朋友",
      "看来我们之间还有许多事情要交流交流",
      "咱两这默契度，真是酒肉朋友的交情啊！",
      "你保住了革命友谊的火种",
      "不错，没白交你这个朋友",
      "看来我们之间还有许多事情要交流交流",
      "我的军大衣！",
      "深知我心",
      "原来最懂我的人就在这里。",
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //  options.question_id=97;
    //  options.id=2;
    if (options.image_url) {
      that.setData({
        image_url: options.image_url,
        user_name: options.user_name,

      })
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          userInfo: wx.getStorageSync('userInfo'),
          question_id: options.question_id,
          hostUserId: wx.getStorageSync('userid')
        })
      }
    })
    if (options.id == 1) {
      getPakege(this)
    } else if (options.id == 2) {
      that.setData({
        ishidden: true
      })
      getAllResult(that);
    }
  },
  toCreat: function () {
    wx.switchTab({
      url: '../../pages/index/index',
    })
  },
  toDraw: function () {
    wx.navigateTo({
      url: '../../pages/withdraw/withdraw',
    })
  },
  hiddenMotai: function () {
    this.setData({
      ishidden: true,
    })
  },
  onShow: function () {

  },
  getAnswer: function () {
    canIGetAnswer(this);
  }
})

function getPakege(that) {
  wx.request({
    url: app.globalData.allUrl + "/api/red_packe.php",
    data: {
      question_id: that.data.question_id,
      user_id: wx.getStorageSync("userid")
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
      console.log(res.data.money)
      getAllResult(that);
      if (res.data.money != "0") {
        that.setData({
          pakegeMoney: res.data.money
        })

      } else if (res.data.money == "0") {
        that.setData({
          hasPakege: false,
        })
      }
    }
  })
}


function getAllResult(that) {
  wx.request({
    url: app.globalData.allUrl + "/api/all_practice.php",
    data: {
      question_id: that.data.question_id,
      // question_id: 42,
      user_id: wx.getStorageSync("userid")
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
      console.log(res)
      var hostPercent;

      hostPercent = (parseFloat(res.data.my) / parseFloat(res.data.all_number) * 100).toFixed(2);
      console.log(hostPercent)
      //   hostPercent=100.00;
      if (parseInt(hostPercent) == 0) {
        that.setData({
          currentWaterImage: that.data.waterImage[0]
        })
      } else if (parseFloat(hostPercent) > 0 && parseFloat(hostPercent) <= 20) {
        that.setData({
          currentWaterImage: that.data.waterImage[1]
        })
      } else if (parseFloat(hostPercent) > 20 && parseFloat(hostPercent) <= 40) {
        that.setData({
          currentWaterImage: that.data.waterImage[3]
        })
      } else if (parseFloat(hostPercent) > 40 && parseFloat(hostPercent) <= 60) {
        that.setData({
          currentWaterImage: that.data.waterImage[5]
        })
      } else if (parseFloat(hostPercent) > 60 && parseFloat(hostPercent) <= 80) {
        that.setData({
          currentWaterImage: that.data.waterImage[7]
        })
      } else if (parseFloat(hostPercent) > 80 && parseFloat(hostPercent) <= 90) {
        that.setData({
          currentWaterImage: that.data.waterImage[9]
        })
      } else if (parseInt(hostPercent) == 100) {
        that.setData({
          currentWaterImage: that.data.waterImage[10]
        })
      }
      for (var i = 0; i < res.data.data.length; i++) {
        res.data.data[i].correct_rate2 = res.data.data[i].correct_rate;
        res.data.data[i].correct_rate = (parseFloat(res.data.data[i].correct_rate) / parseFloat(res.data.all_number) * 100).toFixed(2)
      }
      that.setData({
        allPlayer: res.data.data,
        putUser: res.data.userinfo[0],
        my: hostPercent,
        answerNum: res.data.answer_num,
        all_number: res.data.all_number
      })
    }
  })
}

function navToAnswers(that) {
  wx.navigateTo({
    url: '../../pages/answers/answers?user_id=' + wx.getStorageSync('userid') + "&question_id=" + that.data.question_id,
  })
}
function canIGetAnswer(that) {
  wx.request({
    url: app.globalData.allUrl + "/api/answer_money.php",
    data: {
      question_id: that.data.question_id,
      //  question_id: 42,
      user_id: wx.getStorageSync("userid")
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (bres) {
      console.log(bres.data)
      if (bres.data.state == 200) {
        navToAnswers(that)
      } else if (bres.data.state == 400) {
        wx.showModal({
          title: '提示',
          content: '查看答案需收取1元手续费，点击确认查看答案',
          success: function (res) {
            if (res.confirm) {
              wx.request({
                url: app.globalData.allUrl + "/api/wxpay.php",
                data: {
                  order_id: bres.data.order_id,
                  user_id: wx.getStorageSync("userid"),
                  money: 1
                },
                header: { 'content-type': "application/x-www-form-urlencoded" },
                method: 'POST',
                success: function (rese) {
                  console.log(rese.data)
                  if (rese.data) {
                    var pays = rese.data;
                    wx.requestPayment({
                      timeStamp: pays.timeStamp,
                      nonceStr: pays.nonceStr,
                      package: pays.package,
                      signType: pays.signType,
                      paySign: pays.paySign,
                      success: function (res) {
                        console.log(res)
                        navToAnswers(that);
                      }, fail(res) {
                        wx.showModal({
                          title: '提示',
                          content: '支付失败！',
                        })
                      }
                    })
                  }
                }, fail: function () {
                  wx.showToast({
                    title: '服务器异常',
                    icon: "loading"
                  })
                }
              })
            }
          }
        })
      }
    }
  })
}